package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MapSearchServiceModel extends AndroidViewModel {
    public MutableLiveData<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>> serviceCategory = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> distanceList = new MutableLiveData<>();
    public MutableLiveData<List<EpQuoteEntity>> serviceList = new MutableLiveData<>();
    public MutableLiveData<List<EpQuoteEntity>> providerList = new MutableLiveData<>();

    public MapSearchServiceModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取服务类别
     */
    public void getServiceType() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceCategoryOption().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(getApplication(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> list = response.body().getData();
                    ParentAreaEntity parentAreaEntity = new ParentAreaEntity();
                    parentAreaEntity.setName("全部");
                    parentAreaEntity.setId("");
                    list.add(0, parentAreaEntity);
                    serviceCategory.postValue(list);
                } else {
                    MToast.showToast(getApplication(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Throwable t) {
            }
        });
    }

    public void getDistanceModel() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity entity = new PickSelectEntity("", "不限");
        list.add(entity);
        entity = new PickSelectEntity("10000", "10KM");
        list.add(entity);
        entity = new PickSelectEntity("20000", "20KM");
        list.add(entity);
        entity = new PickSelectEntity("30000", "30KM");
        list.add(entity);
        entity = new PickSelectEntity("40000", "40KM");
        list.add(entity);
        entity = new PickSelectEntity("50000", "50KM");
        list.add(entity);
        entity = new PickSelectEntity("60000", "60KM");
        list.add(entity);
        entity = new PickSelectEntity("70000", "70KM");
        list.add(entity);
        entity = new PickSelectEntity("80000", "80KM");
        list.add(entity);
        entity = new PickSelectEntity("90000", "90KM");
        list.add(entity);
        entity = new PickSelectEntity("100000", "100KM");
        list.add(entity);
        entity = new PickSelectEntity("200000", "200KM");
        list.add(entity);
        entity = new PickSelectEntity("300000", "300KM");
        list.add(entity);
        entity = new PickSelectEntity("400000", "400KM");
        list.add(entity);
        entity = new PickSelectEntity("500000", "500KM");
        list.add(entity);
//        entity = new PickSelectEntity("500000", "500KM以上");
//        list.add(entity);

        distanceList.postValue(list);
    }

    /**
     * 服务列表数据
     */
    public void getServiceList(Context context, Map<String, Object> map) {
        map.put("limit", 20);
        map.put("page", 1);
        map.put("isCompanyOrService", 0);
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getSearchServiceList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EpQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Response<BaseEntity<ListResponse<List<EpQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    serviceList.postValue(response.body().getData().getList());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 服务商列表数据
     */
    public void getProviderList(Context context, Map<String, Object> map) {
        map.put("limit", 20);
        map.put("page", 1);
        map.put("isCompanyOrService", 1);
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getSearchServiceList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EpQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Response<BaseEntity<ListResponse<List<EpQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    providerList.postValue(response.body().getData().getList());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
